layui.use(['admin', 'ax', 'table'], function () {
    var $ax = layui.ax;
    var admin = layui.admin;
    var table = layui.table;

    var Current = {
        tableId: "table"
    };

    Current.initColumn = function () {
        return [[
            {type: 'numbers'},
            {field: 'id', hide: true, sort: true, title: 'id'},
            {field: 'name', sort: true, title: '角色'},
            {align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 200}
        ]];
    };

    Current.search = function () {
        var queryData = {};
        table.reload(Current.tableId, {where:queryData});
    };

    Current.onEdit = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '编辑菜单',
            area: ['60%','70%'],
            content: system.contextPath + '/admin/menuRole/toEdit?id=' + data.id,
            end: function () {
                admin.getTempData('formOk') && table.reload(Current.tableId);
            }
        });
    };

    Current.onAdd = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '添加',
            area: ['60%','70%'],
            content: system.contextPath + '/admin/menuRole/toAdd',
            end: function () {
                admin.getTempData('formOk') && table.reload(Current.tableId);
            }
        });
    };

    Current.onAssign = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '权限编辑',
            area: ['60%','70%'],
            content: system.contextPath + '/admin/menuRole/menu/assign?id=' + data.id,
            end: function () {
                admin.getTempData('formOk') && table.reload(Current.tableId);
            }
        });
    };


    Current.onDataLock = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '数据锁',
            area: ['60%','70%'],
            content: system.contextPath + '/admin/menuRole/to/data/lock/' + data.id,
            end: function () {
                admin.getTempData('formOk') && table.reload(Current.tableId);
            }
        });
    };

    Current.onDelete = function (data) {
        var operation = function () {
            var ajax = new $ax(system.contextPath + "/admin/menuRole/delete/id/" + data.id, function (res) {
                if(res.status){
                    system.success("删除成功!");
                    table.reload(Current.tableId);
                }else{
                    system.error("删除失败!" + res.msg + "!");
                }
            }, function (res) {
                system.error("删除失败!" + res.msg + "!");
            });
            ajax.start();
        };
        system.confirm("是否删除菜单" + data.name + "?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Current.tableId,
        url: system.contextPath + '/admin/menuRole/search/page',
        page: true,
        request: {
            pageName: 'current' //页码的参数名称，默认：page
            , limitName: 'size' //每页数据量的参数名，默认：limit
        },
        height: "full-158",
        cellMinWidth: 100,
        cols: Current.initColumn()  ,parseData: function(res){ //res 即为原始返回的数据
            return {
                "code": res.code, //解析接口状态
                "msg": res.msg, //解析提示文本
                "count": res.data.total, //解析数据长度
                "data": res.data.records //解析数据列表
            };
        }
    });

    // 添加按钮
    $('#btnAdd').click(function () {
        Current.onAdd();
    });

    // 工具条点击事件
    table.on('tool(' + Current.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Current.onEdit(data);
        } else if (layEvent === 'delete') {
            Current.onDelete(data);
        } else if (layEvent === 'assign') {
            Current.onAssign(data);
        }else if (layEvent === 'data_lock'){
            Current.onDataLock(data);
        }
    });
});
